package com.example.luohao.myrecyclerviewdemo;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.zhxu.recyclerview.base.ViewHolder;
import com.zhxu.recyclerview.section.SectionRVAdapter;
import com.zhxu.recyclerview.section.StatelessSection;
import com.zhxu.recyclerview.wrapper.HeaderAndFooterWrapper;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目名称：MyRecyclerViewDemo
 * 类描述：分组加条目头Recycler
 * 创建人：luohao
 * 创建时间：2018/1/31 23:28
 * 修改人：luohao
 * 修改时间：2018/1/31 23:28
 * 修改备注：
 */

public class Activity4 extends AppCompatActivity {

    private RecyclerView mRV1;
    private List<String> stringList1;
    private List<String> stringList2;
    private List<String> stringList3;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.actvity_recycler);
        mRV1 = (RecyclerView) this.findViewById(R.id.mRV1);
        mRV1.setLayoutManager(new LinearLayoutManager(this));
        stringList1 = new ArrayList<>();
        stringList2 = new ArrayList<>();
        stringList3 = new ArrayList<>();
        for (int i = 0; i < 35; i++) {
            stringList1.add("条目" + i);
            stringList2.add("条目" + i * 2);
            stringList3.add("条目" + i * 3);
        }
        SectionRVAdapter sectionRVAdapter = new SectionRVAdapter(this);
        sectionRVAdapter.addSection(new MyDataSection(this, "组一", stringList1));
        sectionRVAdapter.addSection(new MyDataSection(this, "组二", stringList2));
        sectionRVAdapter.addSection(new MyDataSection(this, "组三", stringList3));
        TopWrapper topWrapper = new TopWrapper(sectionRVAdapter);
        mRV1.setAdapter(topWrapper);
    }

    //HeaderAndFooterWrapper  是头的父类
    public class TopWrapper extends HeaderAndFooterWrapper {

        public TopWrapper(RecyclerView.Adapter adapter) {
            super(adapter);
            View view = View.inflate(Activity4.this, R.layout.top, null);
            TextView mTV_title = view.findViewById(R.id.mTV_title);
            mTV_title.setText("标题");
            addHeaderView(view);

        }
    }

    //StatelessSection  添加section的父类
    class MyDataSection extends StatelessSection {

        private Context context;
        private List<String> strings;
        private String title;

        public MyDataSection(Context context, String title, List<String> strList) {
            super(R.layout.item_image_text, R.layout.item_common);
            this.title = title;
            this.context = context;
            this.strings = strList;
        }

        @Override
        public int getContentItemsTotal() {
            return strings.size();
        }

        //条目
        @Override
        public ViewHolder getItemViewHolder(View view, int viewType) {
            return new ViewHolder(context, view);
        }

        //条目赋值
        @Override
        public void onBindItemViewHolder(ViewHolder holder, int position) {
            String s = strings.get(position);
            holder.setText(R.id.mTV_common, s);
        }

        //标题
        @Override
        public ViewHolder getHeaderViewHolder(Context context, View view) {
            return new ViewHolder(context, view);
        }

        //标题赋值
        @Override
        public void onBindHeaderViewHolder(ViewHolder holder) {
            super.onBindHeaderViewHolder(holder);
            holder.setText(R.id.mTV_text, title);
        }
    }
}
